package com.coscon.config;

import com.coscon.filter.XssFilter;
import com.coscon.utils.XssStringJsonDeserializer;
import com.coscon.utils.thymeleafTaglib.IrcaUiDialect;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.MultipartConfigElement;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Web相关配置
 * @author 409390047@qq.com
 * @date 2021/5/9
 * @since 1.0.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 视图控制器
     */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
	}

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler指的是访问路径，addResourceLocations指的是文件放置的目录
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/",
//                "classpath:/resources/", "classpath:/static/", "classpath:/public/");
    }

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");
//        registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/login");
    }




    /**
     * xss过滤拦截器
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setOrder(2);
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setAsyncSupported(true);
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("excludes", "/favicon.ico,/plug-in/*,/css/*,/img/*");
        initParameters.put("isIncludeRichText", "true");
        //排除不需要处理的input的name
        //initParameters.put("extInputNames", "username,password");
        filterRegistrationBean.setInitParameters(initParameters);
        return filterRegistrationBean;
    }

    /**
     * 针对传入参数的json格式的，进行处理
     * @return
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder
                .deserializerByType(String.class, new XssStringJsonDeserializer());
    }




    /**
     * 文件上传配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大
        factory.setMaxFileSize(DataSize.ofBytes(104857600)); // 20MBX1024X1024
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofBytes(104857600));// 20MBX1024X1024
        factory.setFileSizeThreshold(DataSize.ofBytes(104857600));// 20MBX1024X1024

        return factory.createMultipartConfig();
    }


    /*        *
     *国际化，多语言配置，默认为中文
     * @author 409390047@qq.com
     * @date 2019/5/5
     * @param []
     * @return org.springframework.web.servlet.LocaleResolver
     */
    @Bean(name="localeResolver")
    public LocaleResolver localeResolverBean() {
        SessionLocaleResolver lr = new SessionLocaleResolver();
        lr.setDefaultLocale(Locale.CHINA);
        return lr;
    }


    @Bean
    @Lazy(true)
    public IrcaUiDialect ircaUiDialect(){
        return new IrcaUiDialect();
    }






}
